//
//  ZXY_DownFileOperation.m
//  DLNetSchool
//
//  Created by 宇周 on 15/8/22.
//  Copyright (c) 2015年 宇周. All rights reserved.
//

#import "ZXY_DownFileOperation.h"
#import "ZXY_DownloadAction.h"

@interface ZXY_DownFileOperation()<ZXY_DownloadActionDelegate>
{
    }
@property(nonatomic , strong)NSMutableArray  *downStack;
@property(nonatomic , strong)NSMutableArray  *downLoadOrderStack;
//@property(nonatomic , strong)ZXY_DownloadAction *downAction;
@property(nonatomic , strong)ZXY_DownItem *currentItem;
@property(nonatomic , strong)NSOperationQueue *currentQueue;
@end

@implementation ZXY_DownFileOperation

static ZXY_DownFileOperation *SINGLETON = nil;

static bool isFirstAccess = YES;

#pragma mark - Public Method

+ (id)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        isFirstAccess = NO;
        
        SINGLETON = [[super allocWithZone:NULL] init];
    });
    
    return SINGLETON;
}

#pragma mark - Life Cycle

+ (id) allocWithZone:(NSZone *)zone
{
    return [self sharedInstance];
}

+ (id)copyWithZone:(struct _NSZone *)zone
{
    return [self sharedInstance];
}

+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
    return [self sharedInstance];
}

- (id)copy
{
    return [[ZXY_DownFileOperation alloc] init];
}

- (id)mutableCopy
{
    return [[ZXY_DownFileOperation alloc] init];
}

- (id) init
{
    if(SINGLETON){
        return SINGLETON;
    }
    if (isFirstAccess) {
        [self doesNotRecognizeSelector:_cmd];
    }
    self = [super init];
    return self;
}

#pragma mark - 队列操作

- (void)pausedItem:(ZXY_DownItem *)pausedItem withBlock:(void (^) (ZXY_DownItem *item)) pausedBlock
{
    [[ZXY_DownloadAction sharedInstance] pauseItem:pausedItem];
    if(pausedBlock)
    {
        pausedBlock(pausedItem);
    }
}

- (void)cancelItem:(ZXY_DownItem *)cacelItem withBlock:(void(^)(ZXY_DownItem *item)) cancelBlock
{
    [[ZXY_DownloadAction sharedInstance] pauseItem:cacelItem];
    if(cancelBlock)
    {
        cancelBlock(cacelItem);
    }
}
#pragma mark - 下载操作相关
- (void)startDownFile:(ZXY_DownItem *)item
{
    [ZXY_DownloadAction sharedInstance].delegate = self;
    [[ZXY_DownloadAction sharedInstance] addLoadItem:item];
}

- (void)stopAllDownFile
{
    [[ZXY_DownloadAction sharedInstance] cancelDown];
}
#pragma mark - session delegate
- (void)downLoadSuccess:(ZXY_DownItem *)item
{
    NSLog(@"Success");
    if(self.delegate)
    {
        if([self.delegate respondsToSelector:@selector(downLoadSuccessOfItem:)])
        [self.delegate downLoadSuccessOfItem:item];
    }
    
    
}

- (void)downLoadFail:(ZXY_DownItem *)item
{
    
    
    if(self.delegate)
    {
        if([self.delegate respondsToSelector:@selector(failDownItem:)])
        [self.delegate failDownItem:item];
    }
}

- (void)downLoadLength:(int64_t)downSize andExpectLength:(int64_t)expectSize aboutItem:(ZXY_DownItem *)item
{
    double long totalSize = expectSize;
    double long currentSize = downSize;
    float ratio       = currentSize / totalSize;
    NSLog(@"%f",ratio);
    if(self.delegate)
    {
        if([self.delegate respondsToSelector:@selector(giveYouRadio:aboutItem:)])
        [self.delegate giveYouRadio:ratio aboutItem:item];
    }
}

- (void)downLoadCancelItem:(ZXY_DownItem *)item
{
    if(self.delegate)
    {
        if([self.delegate respondsToSelector:@selector(cancelDownItem:)])
        [self.delegate cancelDownItem:item];
    }
    
}


- (void)downLoadCancelSession
{
    if(self.delegate)
    {
        if([self.delegate respondsToSelector:@selector(cancelAllTask)])
        {
            [self.delegate cancelAllTask];
        }
    }
}
@end
